package com.qfmy.mystation.dbHelper;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.qfmy.mystation.bean.Wallpaper;

import java.util.ArrayList;

public class WallpaperDBHelper extends SQLiteOpenHelper {
    public static final int TYPE_COLLECTION = 0;
    public static final int TYPE_DOWNLOAD = 1;
    public static final int TYPE_COLLECTION_DOWNLOAD = 2;

    public WallpaperDBHelper(Context context) {
        super(context, "wallpaper.db", null, 1);
    }

    @Override
    public void onCreate(@NonNull SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE wallpaper (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "tag VARCHAR(100), url VARCHAR(200) UNIQUE, path VARCHAR(200), size INTEGER, " +
                "type INTEGER, create_time TimeStamp NOT NULL DEFAULT (datetime('now', 'localtime')))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public static void insert(Context context, String tag, String url, String path, int size, int type) {
        WallpaperDBHelper helper = new WallpaperDBHelper(context);
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("tag", tag);
        values.put("url", url);
        values.put("path", path);
        values.put("size", size);
        values.put("type", type);
        try {
            db.insertOrThrow("wallpaper", null, values);
        } catch (SQLiteConstraintException e) {
            Toast.makeText(context, "该壁纸已下载/收藏！！！", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
        db.close();
    }

    public static ArrayList<Wallpaper> getAllCollectionWallpaper(Context context) {
        ArrayList<Wallpaper> wallpaperArrayList = new ArrayList<>();
        WallpaperDBHelper helper = new WallpaperDBHelper(context);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("wallpaper", null, "type=?",
                new String[]{String.valueOf(TYPE_COLLECTION)}, null, null, null);
        if (cursor.getCount() != 0) {
            while (cursor.moveToNext()) {
                @SuppressLint("Range") Wallpaper wallpaper = new Wallpaper(cursor.getInt(cursor.getColumnIndex("_id")),
                        cursor.getString(cursor.getColumnIndex("tag")),
                        cursor.getString(cursor.getColumnIndex("url")),
                        cursor.getString(cursor.getColumnIndex("path")) == null ? "" : cursor.getString(cursor.getColumnIndex("path")),
                        cursor.getInt(cursor.getColumnIndex("size")),
                        cursor.getInt(cursor.getColumnIndex("type")),
                        cursor.getString(cursor.getColumnIndex("create_time")));
                wallpaperArrayList.add(wallpaper);
            }
        }
        cursor.close();
        db.close();
        return wallpaperArrayList;
    }

    public static ArrayList<Wallpaper> getAllDownloadWallpaper(Context context) {
        ArrayList<Wallpaper> wallpaperArrayList = new ArrayList<>();
        WallpaperDBHelper helper = new WallpaperDBHelper(context);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("wallpaper", null, "type=?",
                new String[]{String.valueOf(TYPE_DOWNLOAD)}, null, null, null);
        if (cursor.getCount() != 0) {
            while (cursor.moveToNext()) {
                @SuppressLint("Range") Wallpaper wallpaper = new Wallpaper(cursor.getInt(cursor.getColumnIndex("_id")),
                        cursor.getString(cursor.getColumnIndex("tag")),
                        cursor.getString(cursor.getColumnIndex("url")),
                        cursor.getString(cursor.getColumnIndex("path")) == null ? "" : cursor.getString(cursor.getColumnIndex("path")),
                        cursor.getInt(cursor.getColumnIndex("size")),
                        cursor.getInt(cursor.getColumnIndex("type")),
                        cursor.getString(cursor.getColumnIndex("create_time")));
                wallpaperArrayList.add(wallpaper);
            }
        }
        cursor.close();
        db.close();
        return wallpaperArrayList;
    }
}
